/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.server.modules.sys.service.loan;

import com.server.common.persistence.Page;
import com.server.common.service.CrudService;
import com.server.modules.api.vo.LoanDto;
import com.server.modules.sys.dao.loan.LoanDao;
import com.server.modules.sys.dao.member.MemberDao;
import com.server.modules.sys.entity.loan.Loan;
import com.server.modules.sys.entity.loan.LoanMaterialtypeRe;
import com.server.modules.sys.entity.material.MaterialType;
import com.server.modules.sys.entity.member.Member;
import com.server.modules.sys.service.material.MaterialTypeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 贷款Service
 * @author 王磊
 * @version 2016-09-13
 */
@Service
@Transactional(readOnly = true)
public class LoanService extends CrudService<LoanDao, Loan> {
@Resource
private MaterialTypeService materialTypeService;
	@Resource
	private LoanMaterialtypeReService loanMaterialtypeReService;
	@Resource
	private MemberDao memberDao;
	public Loan get(String id) {
		return super.get(id);
	}
	
	public List<Loan> findList(Loan loan) {
		return super.findList(loan);
	}
	
	public Page<Loan> findPage(Page<Loan> page, Loan loan) {
		return super.findPage(page, loan);
	}
	
	@Transactional(readOnly = false)
	public void save(Loan loan) {
		super.save(loan);
	}
	
	@Transactional(readOnly = false)
	public void delete(Loan loan) {
		super.delete(loan);
	}

    public List<MaterialType> getMaterialsByLoanId(String id) {
		LoanMaterialtypeRe query=new LoanMaterialtypeRe();
		query.setLoanId(id);
		List<LoanMaterialtypeRe> reList = loanMaterialtypeReService.findList(query);
		//所有的资料类型
		List<MaterialType> allTypes = materialTypeService.findList(new MaterialType());
		//贷款所需的资料类型
		List<MaterialType> loanNeedTypes =new ArrayList<>();
		//从关系表中获取贷款所需要的资料类型
		for (LoanMaterialtypeRe loanMaterialtypeRe : reList) {
			//关系表里面的id
			String materialTypeId = loanMaterialtypeRe.getMaterialTypeId();
			for (MaterialType materialType2 : allTypes) {
				String parentId2 = materialType2.getParentId();
				String ids = materialType2.getId();
				if(materialTypeId.equals(ids)){
					loanNeedTypes.add(materialType2);
				}
			}
		}
		return loanNeedTypes;
    }

	public Page<Member> checkList(Page<Member> page, Member member){
		member.setPage(page);
		page.setList(memberDao.checkList(member));
		System.out.println(page.getList().size());
		return  page;
	}

	public List<MaterialType> getMaterialTypeNames() {
		return dao.getMaterialTypeNames();

	}

	public List<LoanDto> getMaterialTypeLoans(List<String> list) {
		return dao.getMaterialTypeLoans(list);

	}
}